# -!- coding: utf-8 -!-
import json
import os
from abc import ABC, abstractmethod

from facade.loggerfacade import get_filelogger
from facade.mysqlfacade import MysqlUtiles
from xjlibrary.our_file_dir import BaseDir, ImageFile
from xjlibrary.our_file_dir.zip_file import ZIPFileUtils

logger = get_filelogger("./logs/sipolog.txt")
curpath = BaseDir.get_file_dir_absolute(__file__)
configfile = BaseDir.get_new_path(curpath, "db.ini")


class FileToSql(ABC):

    def __init__(self):
        self.filePath = ""
        self.keys = []
        self.keydicts = {}
        self.datadicts = {}

    def set_filePath(self, filePath):
        self.filePath = filePath
        return self

    def get_file_num(self):
        return BaseDir.get_file_line_num(self.filePath)

    def get_text_file(self, dirPath):
        for filePath in BaseDir.get_dir_all_files(dirPath):
            if BaseDir.get_file_ext_name(filePath, double_ext=False) == "txt":
                yield filePath

    def get_zip_file(self, dirPath):
        for filePath in BaseDir.get_dir_all_files(dirPath):
            if BaseDir.get_file_ext_name(filePath, double_ext=False) == "zip":
                yield filePath

    def get_excel_file(self, dirPath):
        for filePath in BaseDir.get_dir_all_files(dirPath):
            if BaseDir.get_file_ext_name(filePath, double_ext=False) == "xls":
                yield filePath

    def get_json_file(self, dirPath):
        for filePath in BaseDir.get_dir_all_files(dirPath):
            if BaseDir.get_file_ext_name(filePath, double_ext=False) == "big_json":
                yield filePath

    def get_one_ext_file(self, dirPath, extname):
        for filePath in BaseDir.get_dir_all_files(dirPath):
            if BaseDir.get_file_ext_name(filePath, double_ext=False) == extname:
                yield filePath

    def get_keys(self):
        return self.keys

    @abstractmethod
    def deal_file(self):
        pass

    @abstractmethod
    def key_deal(self, key):
        pass

    @abstractmethod
    def creat_sql(self):
        pass


class FileToSql2(FileToSql):

    def deal_file(self):
        self.keys.clear()
        self.datadicts.clear()
        for line in BaseDir.read_line(self.filePath):
            if line == "<REC>":
                continue
            if line == "":
                continue
            listsplit = line.split("=")
            key = listsplit[0]
            self.keys.append(key)
            listsplit.remove(key)
            key = self.key_deal(key)
            self.datadicts[key] = "=".join(listsplit)
        return self.datadicts

    def deal_upline(self, line):
        if line == "":
            return True
        listsplit = line.split("=")
        key = listsplit[0]
        self.keys.append(key)
        listsplit.remove(key)
        key = self.key_deal(key)
        self.datadicts[key] = "=".join(listsplit)

    def deal_strinsg(self, stringlist):
        self.keys.clear()
        self.datadicts.clear()
        upline = ""
        for line in stringlist:
            if line == "<REC>":
                continue
            if line == "":
                continue
            # 如果当前行发现该标志 代表为一行有效的新数据 开始处理上一行
            if line.find(">=") != -1:
                listsplit = line.split("=")
                key = listsplit[0]
                key = self.key_deal(key)
                if key in (
                        '专利号', '申请号', '申请日', '公开（公告）号', '公开（公告）日', '主分类号', '分类号', '名称', '申请（专利权）人', '发明（设计）人', '地址',
                        '国省代码',
                        '专利代理机构', '代理人', '优先权号', '优先权日', '国际申请', '国际公布', '进入国家日期', '分案原申请号', '法律状态', '引证文献', '同族专利项',
                        '摘要',
                        '关键词', '专利类型', '族号', '主权项', '优先权', '专利权状态', '专利权状态代码'):
                    if upline != "":
                        self.deal_upline(upline)
                    upline = line
                else:
                    upline += line
            else:
                upline += line
        self.deal_upline(upline)
        return self.datadicts

    def key_deal(self, key):
        return key.replace("<", "").replace(">", "")

    def creat_sql(self):
        pass

    def creat_json(self):
        return json.dumps(self.datadicts, ensure_ascii=False)

    def write_file(self, filePath, jsonmsg):
        BaseDir.single_add_file(filePath, jsonmsg + "\n")


class FileToSql3(FileToSql):

    def deal_file(self):
        num = 0
        print(self.filePath)
        try:
            for line in BaseDir.read_file_r_mode_yield(self.filePath):
                self.datadicts.clear()
                num += 1
                # print(num)
                if line == "":
                    continue
                dicts = json.loads(line)
                if len(dicts) >= 40:
                    app_no = dicts["申请号"]
                    pub_no = dicts["公开（公告）号"]
                    pri_no = dicts["优先权"]
                    if len(app_no) == 16:
                        type_no = app_no[6]
                    elif len(app_no) == 12:
                        type_no = app_no[4]
                    else:
                        logger.error(self.filePath)
                        logger.error(num)
                        logger.error(app_no)
                        raise Exception("出现错误在app_no的长度上")
                elif len(dicts) == 31:
                    app_no = dicts["申请号"]
                    pub_no = dicts["公开（公告）号"]
                    pri_no = dicts["优先权"]
                    type_no = dicts["专利类型"]
                else:
                    logger.error(self.filePath)
                    logger.error(num)
                    logger.error(len(dicts))
                    raise Exception("出现其他长度")
                self.datadicts["app_no"] = app_no
                self.datadicts["pub_no"] = pub_no
                self.datadicts["pri_no"] = pri_no
                self.datadicts["type_no"] = type_no
                yield self.datadicts
        except:
            print(num)

    def creat_json(self):
        return json.dumps(self.datadicts, ensure_ascii=False)

    def creat_line(self):
        line = "{}\t{}\t{}\t{}".format(self.datadicts["pub_no"],
                                       self.datadicts["app_no"],
                                       self.datadicts["pri_no"],
                                       self.datadicts["type_no"])
        return line

    def creat_sql(self):
        sql = "insert ignore into `article` (`pub_no`,`app_no`,`pri_no`,`type_no`) values (%s,%s,%s,%s)"
        return sql

    def write_file(self, filePath, jsonmsg):
        BaseDir.single_add_file(filePath, jsonmsg + "\n")

    def key_deal(self, key):
        pass

    def exec_sql(self, filePath):
        # 配置文件与默认配置文件key的转换
        keytransformdicts = {"port": "db_port", "host": "db_host", "passwd": "db_pw", "user": "db_user",
                             "db": "db_name",
                             "chartset": "db_charset"}
        # mysql 工具类
        myutil = MysqlUtiles(configfile, "jsontosql", keytransformdicts=keytransformdicts)
        i = 0
        sql = self.creat_sql()
        listpara = []
        for line in BaseDir.read_file_r_mode_yield(filePath):
            i += 1
            dicts = json.loads(line)
            listpara.append([dicts["pub_no"], dicts["app_no"], dicts["pri_no"], dicts["type_no"]])
            if i > 10000:
                myutil.ExeSqlMany(sql, listpara, errExit=True)
                i = 0
                listpara.clear()
        myutil.ExeSqlMany(sql, listpara, errExit=True)
        i = 0
        listpara.clear()


# filetosql3 = FileToSql3()
# cnt = 0
# text = ''
# for filePath in filetosql3.get_json_file("F:\\test1"):
#     filetosql3.set_filePath(filePath)
#     for dicts in filetosql3.deal_file():
#         text += filetosql3.creat_line() + "\n"
#         cnt += 1
#         if cnt % 100000 == 1:
#             BaseDir.single_add_file("F:\\test3\\sql.txt", text)
#             text = ""
#             print('cnt: %d' % cnt)
# BaseDir.single_add_file("F:\\test3\\sql.txt", text)
# print('total: %d' % cnt)

# filetosql3.exec_sql("F:\\test3\\sql.big_json")
#
# filetosql = FileToSql2()
# filePathBool = True
# txtPathBool = True
# for filePath in filetosql.get_zip_file("\\\\192.168.30.186\\down_data\\FTP\\jss_zl\\20181012\\专利元数据\\2018"):
#     print(filePath)
#     # if filePathBool:
#     #     if filePath != "\\\\192.168.30.186\\down_data\\FTP\\jss_zl\\20181012\\专利元数据\\2018\\06\\发明专利著录项包(20180615)\FM-ABS_BIB(20180615).zip":
#     #         continue
#     #     else:
#     #         filePathBool = False
#     zipobj = ZIPFileUtils.get_zip_file(filePath)
#     namelist = ZIPFileUtils.get_name_list(zipobj)
#     # print(namelist)
#     for file in namelist:
#         # if txtPathBool:
#         #     if file != "FM/CN108160105A/ABS_BIB/CN201711315106.0.txt":
#         #         continue
#         #     else:
#         #         txtPathBool = False
#         if BaseDir.get_file_ext_name(file, double_ext=False) == "txt":
#             txtstring = zipobj.read(file).decode("utf-8")
#             filetosql.deal_strinsg(txtstring.split("\n"))
#             if len(filetosql.datadicts) != 31:
#                 print(filePath)
#                 print(file)
#                 logger.info(filePath + "->" + file + ":" + str(BaseString.get_strings_line_num(txtstring, "\n")))
#                 print(filetosql.datadicts)
#                 sys.exit(-1)
#             jsons = filetosql.creat_json()
#             filetosql.write_file("F:\\test1\\sipo.big_json", jsons)
# for filePath in filetosql.get_text_file("\\\\192.168.30.186\\down_data\\FTP\\jss_zl\\20181012\\专利元数据\\2018"):
#     print(filePath)
#     filetosql.set_filePath(filePath)
#     if filetosql.get_file_num() != 32:
#         logger.info(filePath + ":" + filetosql.get_file_num())
#     dicts = filetosql.deal_file()
#     jsons = filetosql.creat_json()
#     filetosql.write_file("F:\\test1\\sipo.big_json", jsons)

filetosql = FileToSql2()
filePathBool = True
txtPathBool = True
for filePath in filetosql.get_zip_file("\\\\192.168.30.186\\down_data\\FTP\\jss_zl\\20181012\\专利元数据\\2018"):
    print(filePath)
    zipobj = ZIPFileUtils.get_zip_file(filePath)
    namelist = ZIPFileUtils.get_name_list(zipobj)
    for file in namelist:
        extname = BaseDir.get_file_ext_name(file, double_ext=False)
        if extname == "gif" or extname == "jpg":
            bytes = zipobj.read(file)
            print(file)
            filename = os.path.split(file)[-1]
            filename = os.extsep.join(filename.split(os.extsep)[:-1])
            print(filename)
            BaseDir.create_dir("F:\\test1\\cover\\")
            ImageFile.img_to_format_jpg(bytes, "F:\\test1\\cover\\"+filename+".jpg")



